import requests
from pathlib import Path
from io import BytesIO
from multiprocessing.dummy import Pool
from urllib.parse import urlparse, parse_qs

from webapp.plugins.image_utils import img2webp
from tools import upload


piclab_url = 'http://192.168.9.15/api/upload'

twitter_headers = {
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.34',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
}
piclab_headers = {
    'Host': 'piclab.org',
    'Origin': 'http://piclab.org',
    'Cookie': 'session=.eJwljkEOwzAIBP_icw-ADYF8JjIGq70mzanq32up11nNaj7lmGdez7K_zzsf5XhF2Ut0iZ4aPGT23AYKp6J1gDpmDx0LKdNatcuGbGFkOmgZ7JNVLQjYxZy9AjWs667Oxq4boaFCWhNobhDuk6I60agxzaWKQ1kh95XnvwbL9wcAtC95.ZNDqnQ.JQvnv2gIMhx-8dGsLC-6aRFn3_g'
}
INDEX = 0

clip_file = Path(__file__).parent / 'clip.txt'
with open(clip_file) as f:
    urls = f.read()
    print(urls)
    print('---------')
    urls = urls.split('\n')

urls_list = set()
for u in urls:
    if u:
        parser = urlparse(u)
        query_params = parse_qs(parser.query)
        img_format = query_params.get('format', ['jpg'])[0]
        origin_url = u.split('?')[0] + f'.{img_format}:orig'
        urls_list.add(origin_url)
print(len(urls_list))


def handler(url):
    try:
        response = requests.get(url, headers=twitter_headers)
    except:
        response = requests.get(url, headers=twitter_headers)
    # 截掉原URL最后的 :orig
    file_path = Path(url[:-5])
    # 原文件名
    file_name = file_path.name
    global INDEX
    INDEX += 1
    print(INDEX, file_name, response.status_code)
    if response.status_code != 200:
        return
    file_obj = BytesIO(response.content)
    img_dict = img2webp(file_obj, quality=85)
    if img_dict['convert']:
        file_name = file_path.stem + '.webp'
    response = upload(file_name, img_dict)
    img_dict['upload_status'] = response['status']
    img_dict['upload_status_alias'] = response['msg']
    print(img_dict)


if __name__ == '__main__':
    with Pool(processes=3) as pool:
        pool.map(handler, urls_list)

    with open(clip_file, 'a') as f:
        f.truncate(0)
